/*
 * Created on 2021年11月8日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.pentaho.di.ui.spoon.navigation.design.classification;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
import org.pentaho.di.core.plugins.BasePluginType;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.pentaho.di.ui.spoon.Spoon;
import org.yzbdl.lanius.core.plugins.service.ClassificationService;

/**
 * 新增插件分组类
 * <p>创建插件栏目新增窗口
 *
 * @author 姚宝林
 * @version 1.1
 * @since 1.0
 */
public class ClassificationAddShell {

    private static Class<?> PKG = Spoon.class;

    private ClassificationService classificationService = ClassificationService.getInstance();

    // /**
    // * 返回栏目新增工具
    // *
    // * @param toolBar
    // * @param parent
    // * @param type
    // * @return
    // */
    // public ToolItem getToolItem(ToolBar toolBar, Shell parent, FuncType type) {
    // ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
    // toolItem.setImage(GUIResource.getInstance().getImageAddClassification());
    // toolItem.addSelectionListener(new SelectionAdapter() {
    //
    // @Override
    // public void widgetSelected(SelectionEvent selectionEvent) {
    // super.widgetSelected(selectionEvent);
    // createAddShell(parent, type);
    // }
    // });
    // return toolItem;
    // }

    /**
     * 返回栏目新增工具
     *
     * @param toolBar
     * @param parent
     * @param type
     * @return
     */
    public ToolItem getToolItem(ToolBar toolBar, Shell parent, Class<? extends BasePluginType> type) {
        ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
        toolItem.setImage(GUIResource.getInstance().getImageAddClassification());
        toolItem.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent selectionEvent) {
                super.widgetSelected(selectionEvent);
                createAddShell(parent, type);
            }
        });
        return toolItem;
    }

    // public void createAddShell(Shell parent, FuncType type) {
    // Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.ON_TOP);
    // shell.setBounds((parent.getSize().x - 260) / 2, (parent.getSize().y - 100) / 2, 260, 100);
    // shell.open();
    // shell.setText(BaseMessages.getString(PKG, "Classification.Shell.New"));
    // Image image = GUIResource.getInstance().getImageShellTitle();
    // shell.setImage(image);
    // Text text = new Text(shell, SWT.BORDER | SWT.CENTER);
    // text.setBounds(10, 10, 130, 25);
    // Button confirmButton = new Button(shell, SWT.PUSH | SWT.CENTER);
    // confirmButton.setBounds(150, 10, 82, 25);
    // confirmButton.setText(BaseMessages.getString(PKG, "System.Button.OK"));
    // confirmButton.addSelectionListener(new SelectionAdapter() {
    //
    // @Override
    // public void widgetSelected(SelectionEvent e) {
    // classificationService.add(text.getText(), type.getId());
    // shell.close();
    // }
    // });
    // }

    /**
     * @描述  添加栏目窗体
     * @data 2020/10/28
     * @param parent
     * @param type
     * @return void
     */
    public void createAddShell(Shell parent, Class<? extends BasePluginType> type) {
        Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.ON_TOP);
        shell.setBounds((parent.getSize().x - 260) / 2, (parent.getSize().y - 100) / 2, 260, 100);
        shell.open();
        shell.setText(BaseMessages.getString(PKG, "Classification.Shell.create"));
        Image image = GUIResource.getInstance().getImageShellTitle();
        shell.setImage(image);
        Text text = new Text(shell, SWT.BORDER | SWT.CENTER);
        text.setBounds(10, 10, 130, 25);
        text.setFocus();
        text.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                switch (e.character) {
                    case SWT.CR:
                        classificationService.addCustomize(text.getText(), type);
                        shell.close();
                        break;
                    case SWT.ESC:
                        shell.close();
                        break;
                }

            }
        });
        Button confirmButton = new Button(shell, SWT.PUSH | SWT.CENTER);
        confirmButton.setBounds(150, 10, 82, 25);
        confirmButton.setText(BaseMessages.getString(PKG, "System.Button.OK"));
        confirmButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                classificationService.addCustomize(text.getText(), type);
                shell.close();
            }
        });
    }
}
